package com.kaka.controller;

import com.google.gson.Gson;
import com.kaka.dao.AdimgDao;
import com.kaka.entity.Adimg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "AdimgServlet",urlPatterns = "/AdimgServelt")
public class AdimgServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        response.setHeader("Access-Control-Allow-Origin","*");

        //编写业务代码 返回的是json格式 并且处理好中文
        response.setContentType("application/json;charset=UTF-8");
        //查询数据库
        AdimgDao dao =new AdimgDao();
        List<Adimg> list = dao.getList();
        //返回数据
        PrintWriter writer = response.getWriter();
        writer.write(new Gson().toJson(list));
        writer.close();
        writer.flush();
    }
}
